package com.chuantou.yuba.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;

import com.chuantou.yuba.R;
import com.chuantou.yuba.ui.fragment.AbsPublishNearFishingFragment;
import com.chuantou.yuba.util.Constants;
import com.chuantou.yuba.util.FragmentUtil;
import com.chuantou.yuba.util.UiUtil;

/**
 * 发布钓场、渔具店相关界面
 * 
 * @author dl1001
 * 
 */
public class PublishNearFishingActivity extends BaseActionBarActivity implements OnClickListener {
	private AbsPublishNearFishingFragment mCurrentFragment;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.publish_near_fishing);
		setupViews();
		String title = getIntent().getStringExtra(Constants.EXTRA_TITLE);
		setTitle(title);
	}

	private void setupViews() {
		switchContentView(getIntent().getStringExtra(Constants.EXTRA_DATA));
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.submit:
			break;
		case R.id.abs__up:
			onBackPressed();
			break;
		default:
			break;
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu_submit, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_submit:
			if (UiUtil.isFastDoubleClick())
				return true;
			if (mCurrentFragment != null) {
				mCurrentFragment.onSubmit();
			}
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	private void switchContentView(String fragmentName) {
		FragmentManager fragmentManager = getSupportFragmentManager();
		FragmentUtil.hideAllFragment(fragmentManager);
		FragmentTransaction transaction = fragmentManager.beginTransaction();
		Fragment fragment = Fragment.instantiate(this, fragmentName);
		fragment.setArguments(getIntent().getExtras());
		transaction.add(R.id.publish_content, fragment, fragmentName);
		mCurrentFragment = (AbsPublishNearFishingFragment) fragment;
		transaction.commitAllowingStateLoss();
	}

}
